﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using System.Data.Linq;
using System.Linq;
namespace Exact_Extension
{
    public partial class DiscountMaster : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        public DiscountMaster(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadData();
        }
        private void LoadData()
        {
            var queryItem = from Items in DS.ISHCMC_V_Items
                            select Items;
            this.iSHCMCVItemBindingSource.DataSource = queryItem;

            var queryDis = from Dis in DS.ISHCMC_DiscountMasters
                           select Dis;
            this.iSHCMCDiscountMasterBindingSource.DataSource = queryDis;
        }
        private bool CheckDiscountTypeExist(string discountname)
        {
            var query = from dis in DS.ISHCMC_DiscountMasters
                        where dis.DiscountName == discountname
                        select dis;
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void gridView_DiscountMD_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            ISHCMC_DiscountMaster itemDis = (ISHCMC_DiscountMaster)this.gridView_DiscountMD.GetRow(this.gridView_DiscountMD.GetSelectedRows()[0]);
            if (itemDis.Percentage.HasValue)
            {
                if (CheckDiscountTypeExist(itemDis.DiscountName))
                {
                    ISHCMC_DiscountMaster itemUpdate = DS.ISHCMC_DiscountMasters.Single(c => c.DiscountName == itemDis.DiscountName);
                    itemUpdate.Percentage = itemDis.Percentage.Value;
                    DS.SubmitChanges();
                }
                else
                {
                    ISHCMC_DiscountMaster itemAdd = new ISHCMC_DiscountMaster
                    {
                        DiscountName = itemDis.DiscountName,
                        ItemImpact = itemDis.ItemImpact,

                        Percentage = itemDis.Percentage.Value
                    };
                    DS.ISHCMC_DiscountMasters.InsertOnSubmit(itemAdd);
                    DS.SubmitChanges();
                }
                this.LoadData();
            }
            else
            {
                MessageBox.Show("Percent haven't fill data yet !!!");
            }
        }
      
    }
}